home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 17.6 KB | 645 lines | [TEXT/CWIE] |
- #include "AnimatedIcons.h"
- #include "LArrayIterator.h"
- #include "IconSuitePatches.h"
- #include <limits.h>
- #include <speech.h>
-
- #ifndef BUILDINGINIT
- #include <iostream.h>
- #endif
-
- #include <stdio.h>
-
- int vdprintf( const char * format, va_list arg);
- int dprintf(const char * format, ...);
-
- // ============================================================================================
-
- unsigned long gNextTickToCheckAnimations = 0;
-
- // ============================================================================================
-
- LArray* gIconsToAnimate = nil;
- LArray* gIconsOnScreen = nil;
-
- // ============================================================================================
-
-
- AnimatedIcon::AnimatedIcon ( void* iconSuiteToReplace ) :
- mIconSuiteWeAreReplacing ( iconSuiteToReplace )
- {
- #ifndef BUILDINGINIT
- cout << "AnimatedIcon::AnimatedIcon() @ " << (void*) this << endl;
- #endif
-
- if ( gIconsToAnimate )
- gIconsToAnimate->InsertItemsAt ( 1, LArray::index_Last, & this, sizeof( this ) );
- }
-
- AnimatedIcon::~AnimatedIcon ( )
- {
- #ifndef BUILDINGINIT
- cout << "AnimatedIcon::~AnimatedIcon() @" << (void*) this << endl;
- #endif
-
- if ( gIconsToAnimate )
- gIconsToAnimate->Remove ( & this, sizeof( this ) );
- }
-
- AnimatedIcon* AnimatedIcon::GetAnimatedIconForIconSuite ( void* iconSuite )
- { AnimatedIcon* animatedIcon = nil;
-
- for ( LArrayIterator iter ( *gIconsToAnimate ); iter.Next( & animatedIcon ); )
- if ( animatedIcon && animatedIcon->mIconSuiteWeAreReplacing == iconSuite )
- return animatedIcon;
-
- return nil;
- }
-
- // ============================================================================================
-
- AnimatedIconOnScreen::AnimatedIconOnScreen ( AnimatedIcon* animatedIcon, GrafPtr port, const Rect* rect, IconAlignmentType align, IconTransformType transform ) :
- mAnimatedIcon ( animatedIcon ),
- mPort ( port ),
- mRect ( *rect ),
- mAlign ( align ),
- mTransform ( transform ),
- mNextAnimationTick ( TickCount() ),
- mInvalidatedCount ( 0 ),
- mBitMap ( nil )
- {
- if ( gIconsOnScreen )
- gIconsOnScreen->InsertItemsAt ( 1, LArray::index_Last, & this, sizeof( this ) );
- }
-
- AnimatedIconOnScreen::~AnimatedIconOnScreen ( )
- {
- if ( gIconsOnScreen )
- gIconsOnScreen->Remove ( & this, sizeof( this ) );
- }
-
- AnimatedIconOnScreen* AnimatedIconOnScreen::GetAnimatedIconOnScreen ( GrafPtr inPort, const Rect& r, void* iconSuiteH )
- { AnimatedIconOnScreen* animatedIcon = nil;
-
- for ( LArrayIterator iter ( *gIconsOnScreen ); iter.Next( & animatedIcon ); )
- if ( animatedIcon && animatedIcon->mAnimatedIcon && animatedIcon->mAnimatedIcon->GetIconSuiteThisReplaces() == iconSuiteH &&
- ( animatedIcon->mPort == inPort ) &&
- * (long*) & animatedIcon->mRect.top == * (long*) & r.top &&
- * (long*) & animatedIcon->mRect.bottom == * (long*) & r.bottom )
- return animatedIcon;
-
- return nil;
- }
-
- // ============================================================================================
-
- FrameAnimatedIcon::FrameAnimatedIcon ( void* iconSuiteToReplace, FrameAnimatedIconResourceHandle aicnHandle ) :
- AnimatedIcon ( iconSuiteToReplace )
- {
- if ( (**aicnHandle).signature == 'KSDC' && (**aicnHandle).version == 1 )
- {
- mTicksPerAnimationCycle = (**aicnHandle).ticksPerAnimationCycle;
-
- mCountOfAnimatedIcons = (**aicnHandle).countOfAnimatedIcons;
-
- for ( unsigned short i = 0; i < mCountOfAnimatedIcons; i ++ )
- {
- CallGetIconSuite ( & mAnimatedIcons[i], (**aicnHandle).icons[i], kSelectorAllAvailableData );
- ForEachIconDo ( mAnimatedIcons[i], svAllAvailableData, DetachIconResource, this );
- }
- }
- }
-
- // ============================================================================================
-
- FrameAnimatedIcon::~FrameAnimatedIcon ( )
- {
- for ( unsigned short i = 0; i < mCountOfAnimatedIcons; i ++ )
- if ( mAnimatedIcons[i] )
- CallDisposeIconSuite ( mAnimatedIcons[i], true );
- }
-
- // ============================================================================================
-
- pascal OSErr FrameAnimatedIcon::DetachIconResource ( OSType resType, Handle* iconHandle, void* yourDataPtr )
- {
- #pragma unused(resType,yourDataPtr)
-
- if ( *iconHandle )
- DetachResource ( *iconHandle );
-
- return noErr;
- }
-
- // ============================================================================================
-
- FrameAnimatedIconOnScreen::FrameAnimatedIconOnScreen ( AnimatedIcon* animatedIcon, GrafPtr port, const Rect* rect, IconAlignmentType align, IconTransformType transform ) :
- AnimatedIconOnScreen ( animatedIcon, port, rect, align, transform ),
- mTicksPerAnimationCycle ( ( (FrameAnimatedIcon*) animatedIcon)->mTicksPerAnimationCycle ),
- mCountOfAnimatedIcons ( ( (FrameAnimatedIcon*) animatedIcon)->mCountOfAnimatedIcons )
- {
- }
-
- FrameAnimatedIconOnScreen::~FrameAnimatedIconOnScreen ( )
- {
- }
-
- void FrameAnimatedIconOnScreen::Advance ( )
- {
- if ( mNextAnimationTick )
- {
- mFrame ++;
-
- if ( mFrame >= GetFrameCount() )
- mFrame = 0;
-
- mNextAnimationTick = TickCount() + GetTicksPerFrame();
- }
- }
-
- OSErr FrameAnimatedIconOnScreen::Draw ( const Rect* rect, IconAlignmentType align, IconTransformType transform )
- {
- mInvalidatedCount = 0;
- return CallPlotIconSuite ( rect, align, transform, GetFrameIconSuiteHandle( mFrame ) );
- }
-
- unsigned long FrameAnimatedIconOnScreen::Idle ( )
- {
- Draw ( & GetRect(), GetAlign(), GetTransform() );
-
- return GetTicksPerFrame();
- }
-
- // ============================================================================================
-
- QuicktimeMovieAnimatedIcon::QuicktimeMovieAnimatedIcon ( void* iconSuiteToReplace, QuicktimeMovieAnimatedIconResourceHandle movInfoH ,const FSSpec* spec, short movieResID ) :
- AnimatedIcon ( iconSuiteToReplace ),
- mMovie ( 0 ),
- mMovieResFileNum ( 0 )
- { OSErr err = -1;
- if ( IsQuicktimeAvailable() )
- {
- if ( (**movInfoH).signature == 'KSDC' && (**movInfoH).version == 1 )
- {
-
- mMovieFlags = (**movInfoH).flags;
-
-
- if ( spec )
- {
- err = OpenMovieFile ( spec, & mMovieResFileNum, fsRdPerm );
-
- if ( err == noErr )
- err = NewMovieFromFile ( & mMovie, mMovieResFileNum, & movieResID, nil, newMovieActive | newMovieDontAskUnresolvedDataRefs, nil );
- }
-
- #ifndef BUILDINGINIT
- if ( err )
- { FSSpec spec;
- short savedResFile = CurResFile();
-
- err = FSMakeFSSpec ( 0, 0, "\pQuickTime Logo Movie Test", & spec );
- if ( err == noErr )
- err = OpenMovieFile( & spec, & mMovieResFileNum, fsRdPerm );
-
- if ( err == noErr )
- err = NewMovieFromFile ( & mMovie, mMovieResFileNum, & movieResID, nil, newMovieActive | newMovieDontAskUnresolvedDataRefs, nil );
-
- UseResFile ( savedResFile );
- }
- #endif
-
- if ( err )
- mMovie = 0;
- }
- }
- }
-
- QuicktimeMovieAnimatedIcon::~QuicktimeMovieAnimatedIcon ()
- {
- if ( mMovie )
- DisposeMovie ( mMovie );
-
- if ( mMovieResFileNum )
- CloseMovieFile ( mMovieResFileNum );
- }
-
- Boolean QuicktimeMovieAnimatedIcon::sEnterMoviesCalledByUs = false;
- Boolean QuicktimeMovieAnimatedIcon::sQuickTimeAvailableAndInitialized = false;
-
- Boolean QuicktimeMovieAnimatedIcon::IsQuicktimeAvailable()
- {
- if ( ! sEnterMoviesCalledByUs )
- { long quicktimeGestaltValue;
-
- if ( Gestalt ( gestaltQuickTime, & quicktimeGestaltValue ) == noErr )
- {
- sQuickTimeAvailableAndInitialized = EnterMovies () == noErr;
- }
-
- sEnterMoviesCalledByUs = true;
- }
-
- return sQuickTimeAvailableAndInitialized;
- }
-
- // ============================================================================================
-
- QuicktimeMovieAnimatedIconOnScreen::QuicktimeMovieAnimatedIconOnScreen ( AnimatedIcon* animatedIcon, GrafPtr port, const Rect* rect, IconAlignmentType align, IconTransformType transform ) :
- AnimatedIconOnScreen ( animatedIcon, port, rect, align, transform )
- { Movie movie = GetParent()->mMovie;
-
- if ( movie )
- {
- SetPort ( mPort );
-
- SetRect ( *rect );
-
- if (GetParent()->mMovieFlags & kPlaySound)
- SetMovieVolume ( movie, kFullVolume );
- else
- SetMovieVolume ( movie, kNoVolume );
-
- StartMovie ( movie );
- }
- }
-
- OSErr QuicktimeMovieAnimatedIconOnScreen::Draw ( const Rect* rect, IconAlignmentType align, IconTransformType transform )
- {
- #pragma unused(rect,align,transform)
- mInvalidatedCount = 0;
- return UpdateMovie ( GetParent()->mMovie );
- }
-
- unsigned long QuicktimeMovieAnimatedIconOnScreen::Idle ()
- { Movie movie = GetParent()->mMovie;
-
- MoviesTask ( movie, 0 );
-
- if ( IsMovieDone ( movie ) )
- GoToBeginningOfMovie ( movie );
-
- return 0;
- }
-
- void QuicktimeMovieAnimatedIconOnScreen::SetRect ( const Rect& rect )
- {
- if ( GetParent() )
- { Rect myRect;
- Movie movie = GetParent()->mMovie;
-
- switch (GetParent()->mMovieFlags & kRectMask)
- {
- case kIconsRect:
- SetMovieBox ( movie, & rect );
- break;
-
- case kParentRect:
- GetMovieBox ( movie, & myRect);
- OffsetRect(&myRect,
- ((rect.left + rect.right)>>1) - ((myRect.left + myRect.right)>>1),
- rect.bottom - myRect.bottom);
- SetMovieBox ( movie, &myRect );
- break;
-
- case kDontDraw:
- {
- RgnHandle theClip = NewRgn ();
- SetMovieClipRgn ( movie, theClip);
- DisposeRgn (theClip);
- }
- break;
-
- case kSpareRectMode:
- SetMovieBox ( movie, & rect );
- break;
- }
- }
- }
-
- // ============================================================================================
-
- void QuicktimeMovieAnimatedIconOnScreen::SetPort ( GrafPtr newPort )
- {
- dprintf ( "QTime::SetPort(%lx)", newPort );
-
- if ( GetParent() )
- SetMovieGWorld ( GetParent()->mMovie, (CGrafPtr) mPort, nil );
- }
-
- // ============================================================================================
-
- PictureAnimatedIcon::PictureAnimatedIcon ( void* iconSuiteToReplace, PictureAnimatedIconResourceHandle picInfoH ) :
- AnimatedIcon ( iconSuiteToReplace )
- {
-
- if ( (**picInfoH).signature == 'KSDC' && (**picInfoH).version == 1 )
- {
- mPictureH = (PicHandle) Get1Resource ( 'PICT', (**picInfoH).pictureResID );
-
- mPictureFlags = (**picInfoH).flags;
-
- if ( mPictureH && *mPictureH )
- {
- DetachResource ( (Handle) mPictureH );
-
- if ( HandleZone( (Handle) mPictureH ) != SystemZone () )
- { Handle h = NewHandleSys ( GetHandleSize ( (Handle) mPictureH ) );
-
- if ( h && *h )
- {
- BlockMoveData ( *mPictureH, *h, GetHandleSize( h ) );
- DisposeHandle ( (Handle) mPictureH );
- mPictureH = (PicHandle) h;
- }
- }
- }
- else
- mPictureH = nil;
-
- }
- }
-
- PictureAnimatedIcon::~PictureAnimatedIcon ()
- {
- if ( mPictureH )
- DisposeHandle( (Handle) mPictureH );
- mPictureH = nil;
- }
-
- // ============================================================================================
-
- PictureAnimatedIconOnScreen::PictureAnimatedIconOnScreen ( AnimatedIcon* animatedIcon, GrafPtr port, const Rect* rect, IconAlignmentType align, IconTransformType transform ) :
- AnimatedIconOnScreen ( animatedIcon, port, rect, align, transform )
- {
- }
-
- PictureAnimatedIconOnScreen::~PictureAnimatedIconOnScreen ( )
- {
- }
-
- #define kParentRect 1
-
- OSErr PictureAnimatedIconOnScreen::Draw ( const Rect* rect, IconAlignmentType align, IconTransformType transform )
- {
- mInvalidatedCount = 0;
-
- if ( GetParent() && GetParent()->mPictureH )
- if ( GetParent()->mPictureFlags & kParentRect )
- {
- Rect myRect = (*(GetParent()->mPictureH))->picFrame;
- OffsetRect(&myRect,
- ((rect->left + rect->right)>>1) - ((myRect.left + myRect.right)>>1),
- rect->bottom - myRect.bottom);
-
- DrawPicture ( GetParent()->mPictureH, &myRect );
- }
- else
- DrawPicture ( GetParent()->mPictureH, rect );
-
- return noErr;
- }
-
- // ============================================================================================
-
- SoundAnimatedIcon::SoundAnimatedIcon ( void* iconSuiteToReplace, SoundAnimatedIconResourceHandle soundInfoH ) :
- AnimatedIcon ( iconSuiteToReplace )
- {
-
- if ( (**soundInfoH).signature == 'KSDC' && (**soundInfoH).version == 1 )
- {
- mSoundH = (SndListHandle) Get1Resource ( 'snd ', (**soundInfoH).soundResID );
- mSoundFlags = (**soundInfoH).flags;
- mMinTicksBetweenSounds = (**soundInfoH).minTimeBetweenSounds;
- mMaxTicksBetweenSounds = (**soundInfoH).maxTimeBetweenSounds;
-
- if ( mSoundH && *mSoundH )
- {
- DetachResource ( (Handle) mSoundH );
-
- if ( HandleZone( (Handle) mSoundH ) != SystemZone () )
- { Handle h = NewHandleSys ( GetHandleSize ( (Handle) mSoundH ) );
-
- if ( h && *h )
- {
- BlockMoveData ( *mSoundH, *h, GetHandleSize( h ) );
- DisposeHandle ( (Handle) mSoundH );
- mSoundH = (SndListHandle) h;
- }
- }
- }
- else
- mSoundH = nil;
-
- }
- }
-
- SoundAnimatedIcon::~SoundAnimatedIcon ()
- {
- if ( mSoundH )
- DisposeHandle( (Handle) mSoundH );
- mSoundH = nil;
- }
-
- // ============================================================================================
-
- static unsigned long RandomBetween ( unsigned long min, unsigned long max )
- {
- if (min > max)
- return min;
- else
- return min + Random() % ( max - min );
- }
-
- static unsigned long min ( unsigned long a, unsigned long b )
- {
- return ( a < b ) ? a : b ;
- }
-
- SoundAnimatedIconOnScreen::SoundAnimatedIconOnScreen ( AnimatedIcon* animatedIcon, GrafPtr port, const Rect* rect, IconAlignmentType align, IconTransformType transform ) :
- AnimatedIconOnScreen ( animatedIcon, port, rect, align, transform )
- {
- if ( SndNewChannel ( & mSoundChannel, 0, 0, nil ) != noErr )
- mSoundChannel = nil;
-
- mNextTickToStartPlayingSound = TickCount() + RandomBetween ( 5, GetParent()->mMinTicksBetweenSounds );
- }
-
- SoundAnimatedIconOnScreen::~SoundAnimatedIconOnScreen ( )
- {
- if ( mSoundChannel )
- SndDisposeChannel ( mSoundChannel, true );
- }
-
- OSErr SoundAnimatedIconOnScreen::Draw ( const Rect* rect, IconAlignmentType align, IconTransformType transform )
- {
- #pragma unused( rect, align, transform )
-
- mInvalidatedCount = 0;
-
- return errForceCallToOriginalPlotIcon;
- }
-
- unsigned long SoundAnimatedIconOnScreen::Idle ()
- {
- if (( mNextTickToStartPlayingSound > TickCount() ) &&
- ( mNextTickToStartPlayingSound != ULONG_MAX ) &&
- (GetParent()->mSoundFlags & kMouseTriger))
- {
- GrafPtr savePort;
- Point mouse;
-
- ::GetPort ( & savePort );
-
- SetPort( GetPort() );
-
- GetMouse(&mouse);
- if ( PtInRect( mouse, & GetRect() ) )
- mNextTickToStartPlayingSound = 0;
-
- SetPort ( savePort );
- }
-
- if ( mNextTickToStartPlayingSound < TickCount() )
- {
- HLock ( (Handle) GetParent()->GetSoundH() );
-
- SndPlay ( mSoundChannel, GetParent()->GetSoundH(), true );
-
- mNextTickToStartPlayingSound = ULONG_MAX;
- }
- else if ( mNextTickToStartPlayingSound == ULONG_MAX )
- { SCStatus statusRec;
-
- OSErr err = SndChannelStatus ( mSoundChannel, sizeof( statusRec ), & statusRec );
-
- if ( err == noErr )
- {
- if ( statusRec.scChannelBusy == false )
- {
- mNextTickToStartPlayingSound = TickCount() + RandomBetween ( GetParent()->mMinTicksBetweenSounds, GetParent()->mMaxTicksBetweenSounds );
-
- HUnlock ( (Handle) GetParent()->GetSoundH() );
- }
- }
- }
-
- return min ( 300, mNextTickToStartPlayingSound - TickCount() );
- }
-
- // ============================================================================================
-
- VoiceAnimatedIcon::VoiceAnimatedIcon ( void* iconSuiteToReplace, VoiceAnimatedIconResourceHandle VoiceInfoH, FSSpec* spec ) :
- AnimatedIcon ( iconSuiteToReplace )
- {
-
- if ( (**VoiceInfoH).signature == 'KSDC' && (**VoiceInfoH).version == 1 )
- {
- if ((**VoiceInfoH).VoiceResID)
- mVoiceH = (Str255**) Get1Resource ( 'STR ', (**VoiceInfoH).VoiceResID );
- else if (spec != nil)
- {
- mVoiceH = ( Str255** ) NewHandle(256);
- BlockMoveData(spec->name, *mVoiceH, spec->name[0]+1);
- }
- else
- {
- mVoiceH = ( Str255** ) NewHandle(256);
- BlockMoveData("\pNo File Name", *mVoiceH, 13);
- }
- mVoiceFlags = (**VoiceInfoH).flags;
- mMinTicksBetweenVoice = (**VoiceInfoH).minTimeBetweenVoice;
- mMaxTicksBetweenVoice = (**VoiceInfoH).maxTimeBetweenVoice;
-
- if ( mVoiceH && *mVoiceH )
- {
- DetachResource ( (Handle) mVoiceH );
-
- if ( HandleZone( (Handle) mVoiceH ) != SystemZone () )
- { Handle h = NewHandleSys ( GetHandleSize ( (Handle) mVoiceH ) );
-
- if ( h && *h )
- {
- BlockMoveData ( *mVoiceH, *h, GetHandleSize( h ) );
- DisposeHandle ( (Handle) mVoiceH );
- mVoiceH = (Str255**) h;
- }
- }
- }
- else
- mVoiceH = nil;
-
- }
- }
-
- VoiceAnimatedIcon::~VoiceAnimatedIcon ()
- {
- if ( mVoiceH )
- DisposeHandle( (Handle) mVoiceH );
- mVoiceH = nil;
- }
-
-
- VoiceAnimatedIconOnScreen::VoiceAnimatedIconOnScreen ( AnimatedIcon* animatedIcon, GrafPtr port, const Rect* rect, IconAlignmentType align, IconTransformType transform ) :
- AnimatedIconOnScreen ( animatedIcon, port, rect, align, transform )
- {
-
- if ( NewSpeechChannel ( nil, & mVoiceChannel ) != noErr )
- mVoiceChannel = nil;
-
- mNextTickToStartPlayingVoice = TickCount() + RandomBetween ( 5, GetParent()->mMinTicksBetweenVoice );
- }
-
- VoiceAnimatedIconOnScreen::~VoiceAnimatedIconOnScreen ( )
- {
- if ( mVoiceChannel )
- DisposeSpeechChannel ( mVoiceChannel );
- }
-
- OSErr VoiceAnimatedIconOnScreen::Draw ( const Rect* rect, IconAlignmentType align, IconTransformType transform )
- {
- #pragma unused( rect, align, transform )
-
- return errForceCallToOriginalPlotIcon;
- }
-
- unsigned long VoiceAnimatedIconOnScreen::Idle ()
- {
- if (( mNextTickToStartPlayingVoice > TickCount() ) &&
- ( mNextTickToStartPlayingVoice != ULONG_MAX ) &&
- (GetParent()->mVoiceFlags & kMouseTriger))
- {
- GrafPtr savePort;
- Point mouse;
-
- ::GetPort ( & savePort );
-
- SetPort( GetPort() );
-
- GetMouse(&mouse);
- if (PtInRect(mouse, &GetRect()))
- mNextTickToStartPlayingVoice = 0;
-
- SetPort (savePort);
- }
-
- if ( mNextTickToStartPlayingVoice < TickCount() )
- {
- HLock ( (Handle) GetParent()->GetVoiceH() );
-
- SpeakString ( (unsigned char *) *(GetParent()->GetVoiceH()));
-
- mNextTickToStartPlayingVoice = ULONG_MAX;
- }
- else if ( mNextTickToStartPlayingVoice == ULONG_MAX )
- if ( 0 == SpeechBusySystemWide () )
- {
- mNextTickToStartPlayingVoice = TickCount() + RandomBetween ( GetParent()->mMinTicksBetweenVoice, GetParent()->mMaxTicksBetweenVoice );
-
- }
-
- return min ( 300, mNextTickToStartPlayingVoice - TickCount() );
- }
-
- // ============================================================================================
-